home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / lib / mlib35d.zoo / sync.c < prev   
Encoding:
C/C++ Source or Header  |  1993-09-08  |  2.2 KB  |  125 lines

  1. /*
  2.  * FILE
  3.  *    sync.c
  4.  *    
  5.  *    
  6.  * DESCRIPTION
  7.  *    syncing filesystems, makes only sense with mint and
  8.  *    minixfs for now
  9.  *    
  10.  * BUGS
  11.  *    minixfs V 060 PL 5 always syncs all drives, so there will
  12.  *    be to much syncing, since we call Dcntl for all known drives.
  13.  *    
  14.  * $Revision: 1.1 $   $Date: 1993/07/16 16:47:32 $    $Author: ole $
  15.  * 
  16.  * $Log: sync.c,v $
  17.  * Revision 1.1  1993/07/16  16:47:32  ole
  18.  * Initial revision
  19.  *
  20.  */
  21.  
  22. #include <mintbind.h>
  23. #include <stat.h>
  24. #include <errno.h>
  25. #include <support.h>
  26.  
  27. extern int __mint;
  28.  
  29. /* from minixfs.h by S N Henson*/
  30. #define MFS_BASE    0x100
  31. #define MFS_VERIFY    (MFS_BASE)        /* Return minixfs magic number */
  32. #define MFS_SYNC    (MFS_BASE|0x01)    /* Sync the filesystem */
  33. #define MFS_MAGIC    0x18970431        /* Magic number from MFS_VERIFY */
  34.  
  35. /*
  36.  * FUNCTION
  37.  *    int sync(void)
  38.  *    
  39.  * DESCRIPTION
  40.  *    query all known drives for a valid MinixFs
  41.  *    if we find one, sync it.
  42.  */
  43. int sync(void)
  44. {
  45.     long            magic;
  46.     unsigned long    drives;
  47.     int                i, rv;
  48.     char            path[] = "A:\\";
  49.     
  50.     if (!__mint)
  51.         return 0;
  52.  
  53.     drives = Dsetdrv(Dgetdrv());
  54.  
  55.     drives &= ~0x3;    /* don't sync the floppys */
  56.     
  57.     for (i = 2; drives ; i++) {
  58.         if (drives & (1L << i)) {
  59.             drives &= ~(1L << i);
  60.             path[0] = 'A' + i;
  61.             magic = 0L;
  62.             if (!Dcntl(MFS_VERIFY, path, &magic) && magic == MFS_MAGIC) {
  63.                 if ((rv = Dcntl(MFS_SYNC, path, 0L)) < 0) {
  64.                     errno = -rv;
  65.                     return -1;
  66.                 }
  67.             }
  68.         }
  69.     }
  70.         
  71.     return 0;
  72. }  /* sync() */
  73.  
  74.  
  75.  
  76. /*
  77.  * FUNCTION
  78.  *    int fsync(int fd)
  79.  *    
  80.  * DESCRIPTION
  81.  *    sync all buffers related to file descriptor fd
  82.  *    since MFS 605 always syncs all the buffers, we don't bother
  83.  *    to get the full path.
  84.  */
  85. int fsync(fd)
  86.     int    fd;
  87. {
  88.     int            rv;
  89.     long        magic     = 0L;
  90.     char        path[]    = "A:\\";
  91.     struct stat    statbuf;
  92.  
  93.     if (!__mint)
  94.         return 0;
  95.     
  96.     if (fstat(fd, &statbuf))
  97.         return -1;            /* errno set from fstat */
  98.  
  99.     path[0] = 'A'+ statbuf.st_dev;
  100.     if (!Dcntl(MFS_VERIFY, path, &magic) && magic == MFS_MAGIC) {
  101.         if ((rv = Dcntl(MFS_SYNC, path, 0L)) < 0) {
  102.             errno = -rv;
  103.             return -1;
  104.         }
  105.     }
  106.  
  107.     return 0;
  108. }  /* fsync() */
  109.  
  110.  
  111. #ifdef TEST
  112.  
  113. /*
  114.  * Im not in the mood to write a tricky test routine,
  115.  * so just do 'cat junk1 >junk2;sync' from your shell
  116.  * and listen to your harddisk.
  117.  */
  118. int main (void)
  119. {
  120.     sync();
  121.     return 0;
  122. }
  123.  
  124. #endif
  125.